package com.uyayo.framework.captcha.util;

import com.uyayo.framework.captcha.constants.CaptchaConstants;
import com.uyayo.framework.captcha.exception.CaptchaException;
import com.uyayo.framework.captcha.exception.CaptchaTimeOutException;
import com.uyayo.framework.captcha.model.ClickPoint;

import java.awt.*;
import java.util.List;

/**
 * 文字点击验证码
 * @author hfj
 * @date 2018/10/20
 */
public class ClickTextCaptchaUtils{

    /**
     * 检查文字点击验证码
     * @param points 被击点
     * @param rectangles 矩形集合
     * @return 是否验证成功
     */
    public static boolean checkClickTextCaptcha(List<ClickPoint> points, List<Rectangle> rectangles,int rectangleExpandRangeX,int rectangleExpandRangeY){
        for(int i = 0 ; i<points.size(); i++){
            Rectangle rectangle=rectangles.get(i);
            if (points.get(i).getX() < rectangle.getX()-rectangleExpandRangeX
                    || points.get(i).getX() > rectangle.getX()+rectangle.getWidth()+rectangleExpandRangeX
                    || points.get(i).getY() < rectangle.getY() - rectangle.getHeight()-rectangleExpandRangeY
                    || points.get(i).getY() > rectangle.getY()+rectangleExpandRangeY) {
                return false;
            }
        }
        return true;
    }


    /**
     * 验证码校验
     * 默认验证码超时时间 10 分钟
     *
     * @param clickPoints           验证码
     * @param rectangles    session中保存的验证码
     * @param captchaTimeObject 验证码记录的时间
     * @throws CaptchaTimeOutException 验证码超时
     * @throws CaptchaException        验证码错误
     */
    public static void verifyClickTextCaptcha(List<ClickPoint> clickPoints, List<Rectangle> rectangles, Object captchaTimeObject) throws CaptchaException {
        verifyClickTextCaptcha(clickPoints, rectangles, captchaTimeObject, CaptchaConstants.CAPTCHA_TIMEOUT);
    }

    /**
     * 验证码校验
     * 默认验证码超时时间 10 分钟
     *
     * @param clickPoints           验证码
     * @param rectangles    session中保存的验证码
     * @param captchaTimeObject 验证码记录的时间
     * @param rectangleExpandRangeX 扩大矩形校验范围X轴
     * @param rectangleExpandRangeY 扩大矩形校验范围Y轴
     * @throws CaptchaTimeOutException 验证码超时
     * @throws CaptchaException        验证码错误
     */
    public static void verifyClickTextCaptcha(List<ClickPoint> clickPoints, List<Rectangle> rectangles, Object captchaTimeObject,int rectangleExpandRangeX,int rectangleExpandRangeY) throws CaptchaException {
        verifyClickTextCaptcha(clickPoints, rectangles, captchaTimeObject, CaptchaConstants.CAPTCHA_TIMEOUT,rectangleExpandRangeX,rectangleExpandRangeY);
    }

    /**
     * 验证码校验
     * 默认验证码超时时间 10 分钟
     *
     * @param clickPoints           验证码
     * @param rectangles    session中保存的验证码
     * @param captchaTimeObject 验证码记录的时间
     * @param timeOut           验证码超时时间 毫秒
     * @throws CaptchaTimeOutException 验证码超时
     * @throws CaptchaException        验证码错误
     */
    public static void verifyClickTextCaptcha(List<ClickPoint> clickPoints, List<Rectangle> rectangles, Object captchaTimeObject, long timeOut) throws CaptchaException {
        if (null == rectangles || null == captchaTimeObject) {
            throw new CaptchaException("Captcha is null");
        }
        long captchaTime = (Long) captchaTimeObject;
        if (System.currentTimeMillis() - captchaTime > timeOut) {
            throw new CaptchaTimeOutException("captcha timeout");
        }

        boolean result = ClickTextCaptchaUtils.checkClickTextCaptcha(clickPoints, rectangles,0,0);
        if(!result){
            throw new CaptchaException("captcha exitCode error");
        }
    }


    /**
     * 验证码校验
     * 默认验证码超时时间 10 分钟
     *
     * @param clickPoints           验证码
     * @param rectangles    session中保存的验证码
     * @param captchaTimeObject 验证码记录的时间
     * @param timeOut           验证码超时时间 毫秒
     * @param rectangleExpandRangeX 扩大矩形校验范围X轴
     * @param rectangleExpandRangeY 扩大矩形校验范围Y轴
     * @throws CaptchaTimeOutException 验证码超时
     * @throws CaptchaException        验证码错误
     */
    public static void verifyClickTextCaptcha(List<ClickPoint> clickPoints, List<Rectangle> rectangles, Object captchaTimeObject, long timeOut,int rectangleExpandRangeX,int rectangleExpandRangeY) throws CaptchaException {
        if (null == rectangles || null == captchaTimeObject) {
            throw new CaptchaException("Captcha is null");
        }
        long captchaTime = (Long) captchaTimeObject;
        if (System.currentTimeMillis() - captchaTime > timeOut) {
            throw new CaptchaTimeOutException("captcha timeout");
        }

        boolean result = ClickTextCaptchaUtils.checkClickTextCaptcha(clickPoints, rectangles,rectangleExpandRangeX,rectangleExpandRangeY);
        if(!result){
            throw new CaptchaException("captcha exitCode error");
        }
    }
}
